package org.films.domain;

import javax.persistence.Entity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.entity.RooEntity;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import java.util.Calendar;
import java.util.Date;

import javax.persistence.JoinTable;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.springframework.format.annotation.DateTimeFormat;
import java.math.BigDecimal;
import org.films.domain.Director;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;
import java.util.Set;
import org.films.domain.Category;
import java.util.HashSet;
import javax.persistence.ManyToMany;
import javax.persistence.CascadeType;
import org.films.domain.Actor;
import org.films.domain.Study;
import org.films.domain.Comment;

@Entity
@RooJavaBean
@RooToString
@RooEntity(finders = { "findFilmsByStudyAndReleaseYearEquals", "findFilmsByActors" })
public class Film {

    @NotNull
    @Size(max = 1000)
    private String description;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "S-")
    private Date releaseYear;

    @NotNull
    @Size(max = 255)
    private String title;

    private BigDecimal totalSum;

    private Double rating;

    @ManyToOne(targetEntity = Director.class)
    @JoinColumn
    private Director director;
    
    @ManyToMany(cascade = {CascadeType.REFRESH, CascadeType.PERSIST})
    @JoinTable(name = "CATEGORY_FILM", joinColumns = { @JoinColumn(name = "FILM_ID") }, inverseJoinColumns = @JoinColumn(name = "CATEGORY_ID"), uniqueConstraints = { @UniqueConstraint(columnNames = {
			"FILM_ID", "CATEGORY_ID" }) })
    private Set<Category> categories = new HashSet<Category>();

    @ManyToMany(cascade = {CascadeType.REFRESH, CascadeType.PERSIST})
    @JoinTable(name = "FILM_ACTOR", joinColumns = { @JoinColumn(name = "FILM_ID") }, inverseJoinColumns = @JoinColumn(name = "ACTOR_ID"), uniqueConstraints = { @UniqueConstraint(columnNames = {
			"ACTOR_ID", "FILM_ID" }) })
    private Set<Actor> actors = new HashSet<Actor>();

    @ManyToOne(targetEntity = Study.class)
    @JoinColumn
    private Study study;

    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "film")
    private Set<Comment> comments = new HashSet<Comment>();    
}
